package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean
import com.elite.groovy.util.Util
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import java.text.SimpleDateFormat
/*
 * 自动抓取CRM名单数据
 * 每天凌晨两点执行
 */

public class JobDealCrmCustomer implements ServiceBackInvoke{
	private static final Log log =LogFactory.getLog(JobDealCrmCustomer.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String useTimeInfo = "";
		Long useStartTime = System.currentTimeMillis();
		log.debug("自动抓取CRM数据自动任务开始:" + df.format(new Date()));
		def param=new String[0];
		def procedure="{call Sp_Deal_Customer_Crm(?)}";
		def result=eliteBean.executeWithReturn(procedure,param,"MdataSource");
		
		Long useEndTime = System.currentTimeMillis();
		useTimeInfo = Long.toString(useEndTime - useStartTime);
		log.debug(" 处理时长(毫秒):" + useTimeInfo + " EndTime:" + df.format(new Date()));
		if(result=="SUCC"){
		String charset = "utf-8"; //设置字符集
		int timeout = 100000; //设置请求超时时间
		int enableSSLflag = 0;	
		Map headMap=new HashMap(); //设置头文件
		headMap.put("Content-Type","application/json");
		String I_Sync_Type="CDXY"; //接口类型
		String selectSQL1="select WEBSERVICEURL from sysc_interface where webtype='CDXY'";
		def postURL=eliteBean.chaxun_sql(selectSQL1, "dataSource");
		def httpURL=postURL[0].get("WEBSERVICEURL");
		log.debug("车代携续接口地址:"+httpURL);
		StringBuffer buff = new StringBuffer("select r1.rewid rid from crmservice.customer_crm r1");
		buff.append(" where r1.create_time > Trunc(Sysdate - 1) ");
		buff.append(" and r1.customer_guid is not null and r1.rewid is not null");
		def resultlist2= eliteBean.chaxun_sql(buff.toString(), "dataSource");
		log.debug("数据查询sql:"+buff.toString());
						JSONArray jsonArray2=JSONArray.fromObject(resultlist2);
						int ListNum2 = jsonArray2.size();
						JSONArray arr = new JSONArray();
						JSONObject obj = new JSONObject();
						log.debug("打印获取的结果集jsonArray2:"+jsonArray2);
						if(ListNum2>0){
						for(int j=0;j<ListNum2;j++) {
							JSONObject dataObject = jsonArray2.getJSONObject(j);
							log.debug("数据查询dataObject:"+dataObject);
							def rewId=GetJV(dataObject, "RID");
							log.debug("数据查询rewid:"+rewId);
							//JSONObject policyTranTeleList = new JSONObject();
							//policyTranTeleList.put("rewId", GetJV(dataObject, "rewid"));
							obj.put("rewId", rewId);
							arr.add(obj);
						}
						JSONObject objtwo = new JSONObject();
						objtwo = new JSONObject();
						objtwo.put("policyTranTeleList", arr);
						long time=System.currentTimeMillis();
						def postXml="{\"data\":"+objtwo.toString()+",\n" +"\"requestId\":\""+time+"\",\n"+"\"timeStamp\":\""+df.format(time)+"\",\n"+"\"requestType\":\"DEPT_CHANG\"}";
                        //log.debug("请求报文1:"+postXml);
                        def reJson = Util.doHttpPostUseHead(httpURL,postXml,charset,timeout,enableSSLflag,headMap);							
						//def reJson = HttpClientProxy.sendPost(httpURL, postXml,proxyIp,proxyPort);
						JSONObject json = new JSONObject().fromObject(reJson);
						//log.debug("调用接口返回结果:"+reJson);

						if(json.code == "0") {
							log.debug("接口调用结果:"+json.code);

						}
						
						//def insertSQL="insert into qt_dm_ifc_log_new(id,offer_no,ifc_no,send_info,return_info,send_time,return_time) //values (Sys_Guid(),Sys_Guid(),'" + I_Sync_Type + "','" + postXml + "','" + reJson + "',sysdate,sysdate)";
						//log.debug("insertSQL:"+insertSQL);
						//eliteBean.update(insertSQL,'dataSource');
						def insertSQL="DECLARE  clobValue qt_dm_ifc_log_new.Send_Info%TYPE; clobValue1 qt_dm_ifc_log_new.return_info%TYPE; BEGIN  clobValue:='" + postXml + "' ; clobValue1:='" + reJson + "' ;   insert into qt_dm_ifc_log_new(id,offer_no,ifc_no,send_info,return_info,send_time,return_time) values (Sys_Guid(),Sys_Guid(),'" + I_Sync_Type + "',clobValue,clobValue1,sysdate,sysdate);   COMMIT; END;";
						eliteBean.update(insertSQL,'dataSource');						
		}
	}		
}
public static String GetJV(JSONObject inJson, String getKey) {
		String Rev_Value = null
		try {
			Rev_Value = inJson.getString(getKey)
			//log.debug("GetJV 获取参数值(String)-" + getKey + ":" + Rev_Value);
		} catch (Exception e) {
			try {
				Rev_Value = String.valueOf(inJson.getInt(getKey))
				//log.debug("GetJV 获取参数值(Integer)-" + getKey + ":" + Rev_Value);
			} catch (Exception e1) {
				Rev_Value = ""
				//log.debug("GetJV 获取参数值Exception-" + getKey + ":" + Rev_Value);
			}
		} finally {
			if (Rev_Value == null || Rev_Value.equals("null") || Rev_Value.length() <= 0) {
				//log.debug("GetJV 获取参数值Exception-" + getKey + ":" + Rev_Value);
				Rev_Value = ""
			}
		}
		return Rev_Value
	}
}
